#include "loginwgt.h"
#include <QMouseEvent>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
#include <QHBoxLayout>
#include <QLineEdit>
#include "styleloader.h"
#include "ICMS.h"
#include "AppConfig.h"
#include "httphelper.h"
LoginWgt::LoginWgt(QWidget *parent) : QWidget(parent)
{
    setWindowFlag(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_StyledBackground);
    setFixedSize(300,400);
    initLayout();
}

void LoginWgt::slot_Operator(const REQUESTTYPE type, bool result)
{
    switch(type){
    case REGISTER:
        handleRegister(result);
        break;
    case LOGIN:
        handleLogin(result);
        break;
    default:
        break;
    }
}

void LoginWgt::slot_MediaInfo(const QVector<QVariantMap> mediainfo)
{
    //
    if(mediainfo.isEmpty())
    {
        ICMS* icms=new ICMS();
        icms->show();
        hide();
    }
}

void LoginWgt::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton && isPressed)
    {
        if(!qobject_cast<QPushButton*>(childAt(event->pos())))
        {
            move(event->globalPos()-point);
        }
    }
    QWidget::mouseMoveEvent(event);
}

void LoginWgt::mousePressEvent(QMouseEvent *event)
{
    if(!qobject_cast<QPushButton*>(childAt(event->pos())))//判断当前鼠标位置不是按钮
    {
        isPressed=true;
        point=event->globalPos()-this->frameGeometry().topLeft();
    }
    QWidget::mousePressEvent(event);
}

void LoginWgt::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        isPressed=false;
    }
    QWidget::mouseReleaseEvent(event);
}

void LoginWgt::handleLogin(bool result)
{
    if(result)
    {
        AppConfig* app=AppConfig::instance();
        HttpHelper* httpHelper=HttpHelper::instance();
        QVector<QVariantMap>infos=app->getVideoFiles();
        for(const QVariantMap& video : infos)
        {
            if(video.contains("mediaUrl")&&video.contains("mediaTitle"))
            {
                QString mediaUrl = video["mediaUrl"].toString();
                QString mediaTitle = video["mediaTitle"].toString();
                httpHelper->slot_AddmediaInfo(video);
                qDebug()<<"媒体文件："<<mediaTitle<<"路径："<<mediaUrl;
            }
            else
            {
                qDebug()<<"缺少必要字段";
            }
        }
        httpHelper->slot_GetmediaInfo();
    }
    else
    {
        qDebug()<<"登录失败";
    }
}

void LoginWgt::handleRegister(bool result)
{
    if(result)
    {
        stackedWgt->setCurrentWidget(loginWgt);
    }
    else
    {
        qDebug()<<"注册失败";
    }
}

void LoginWgt::initLayout()
{
    //主布局
    QVBoxLayout* mainlayout=new QVBoxLayout(this);
    //创建公共窗口（标题窗口，用于关闭程序）
    QWidget* headerWgt=new QWidget(this);
    headerWgt->setFixedSize(300,150);
    headerWgt->setWindowFlag(Qt::FramelessWindowHint);
    headerWgt->setAttribute(Qt::WA_StyledBackground);
    mainlayout->setObjectName("mainlayout");

    //登录用户头像
    QLabel* iconLabel=new QLabel(headerWgt);
    iconLabel->setAlignment(Qt::AlignCenter);
    QPushButton* closeBtn = new QPushButton(headerWgt);
    iconLabel->setFixedSize(80,80);
    closeBtn->setFixedSize(30,30);
    closeBtn->setObjectName("closeBtn");
    iconLabel->setPixmap(QPixmap("D:/Project/ICMS/picture/icon.png"));
    //头像图片根据窗口缩放
    iconLabel->setScaledContents(true);


    //关闭主程序
    connect(closeBtn,&QPushButton::clicked,this,[this](){
        close();
    });

    //headerWgt布局
    QVBoxLayout* headerlayout = new QVBoxLayout(headerWgt);
    headerlayout->setContentsMargins(0,0,0,0);
    headerlayout->setSpacing(0);

    //顶部水平布局
    QHBoxLayout* toplayout = new QHBoxLayout();
    toplayout->setContentsMargins(0,0,0,0);
    toplayout->addStretch();
    toplayout->addWidget(closeBtn);
    headerlayout->addLayout(toplayout);

    //中间间隔
    headerlayout->addStretch();

    //图标布局
    QHBoxLayout* iconlayout=new QHBoxLayout();
    iconlayout->setContentsMargins(0,0,0,20);
    iconlayout->addStretch();
    iconlayout->addWidget(iconLabel);
    iconlayout->addStretch();
    headerlayout->addLayout(iconlayout);

    headerWgt->setLayout(headerlayout);

    //stackedWidget
    stackedWgt=new QStackedWidget(this);
    stackedWgt->setFixedSize(300,250);
    stackedWgt->setWindowFlag(Qt::FramelessWindowHint);
    stackedWgt->setAttribute(Qt::WA_StyledBackground);

    mainlayout->setSpacing(0);
    mainlayout->addWidget(headerWgt);
    mainlayout->addWidget(stackedWgt);
    mainlayout->setContentsMargins(0,0,0,0);

    //登录窗口
    loginWgt=new QWidget(stackedWgt);
    registerWgt=new QWidget(stackedWgt);

    loginWgt->setFixedSize(300,250);
    loginWgt->setWindowFlag(Qt::FramelessWindowHint);
    loginWgt->setAttribute(Qt::WA_StyledBackground);

    registerWgt->setFixedSize(300,250);
    registerWgt->setWindowFlag(Qt::FramelessWindowHint);
    registerWgt->setAttribute(Qt::WA_StyledBackground);

    //添加登录窗口子控件
    QLineEdit* account=new QLineEdit(loginWgt);
    QLineEdit* passwd=new QLineEdit(loginWgt);
    QPushButton* loginBtn=new QPushButton("登录",loginWgt);
    QPushButton* registerBtn=new QPushButton("没有账户？去注册",loginWgt);

    account->setFixedSize(250,40);
    passwd->setFixedSize(250,40);
    loginBtn->setFixedSize(250,40);
    registerBtn->setFixedSize(250,40);

    account->setPlaceholderText("  username");
    passwd->setPlaceholderText("  passwd");
    passwd->setEchoMode(QLineEdit::Password);

    //登录窗口子控件布局
    QVBoxLayout* loginlayout=new QVBoxLayout(loginWgt);
    loginlayout->setAlignment(Qt::AlignCenter);
    loginlayout->setSpacing(10);
    loginlayout->addWidget(account);
    loginlayout->addWidget(passwd);
    loginlayout->addStretch();
    loginlayout->addWidget(loginBtn);
    loginlayout->addWidget(registerBtn);
    loginlayout->addStretch();
    loginlayout->setContentsMargins(0,0,0,0);
    loginWgt->setLayout(loginlayout);
    //注册子控件
    QLineEdit* reusername=new QLineEdit(registerWgt);
    QLineEdit* repasswd=new QLineEdit(registerWgt);
    QPushButton* startRegisterBtn=new QPushButton("开始注册",registerWgt);
    QPushButton* reloginBtn=new QPushButton("返回登录",registerWgt);

    connect(reloginBtn,&QPushButton::clicked,this,[this](){
        stackedWgt->setCurrentWidget(loginWgt);
    });


    //设置注册窗口子控件大小
    reusername->setFixedSize(250,40);
    repasswd->setFixedSize(250,40);
    startRegisterBtn->setFixedSize(250,40);
    reloginBtn->setFixedSize(250,40);

    //编辑框设置占位符
    reusername->setPlaceholderText("  reusername");
    repasswd->setPlaceholderText("  repasswd");
    repasswd->setEchoMode(QLineEdit::Password);

    //注册窗口设置布局
    QVBoxLayout* registerlayout=new QVBoxLayout(registerWgt);
    registerlayout->setAlignment(Qt::AlignCenter);
    registerlayout->setSpacing(10);
    registerlayout->addWidget(reusername);
    registerlayout->addWidget(repasswd);
    registerlayout->addStretch();
    registerlayout->addWidget(startRegisterBtn);
    registerlayout->addWidget(reloginBtn);
    registerlayout->addStretch();
    registerlayout->setContentsMargins(0,0,0,0);
    registerWgt->setLayout(registerlayout);

    //stackedwidget添加子窗口
    stackedWgt->addWidget(loginWgt);
    stackedWgt->addWidget(registerWgt);

    connect(loginBtn,&QPushButton::clicked,this,[=](){
        QVariantMap infoMap;
        QString username = account->text();
        QString userpasswd= passwd->text();
        if(!username.isEmpty()&&!userpasswd.isEmpty())
        {
            infoMap["userName"]=username;
            infoMap["passwd"]=userpasswd;
            infoMap["nickName"]="test";
            HttpHelper::instance()->slot_Login(infoMap);
        }
        else
        {
            qDebug()<<"账号密码不能为空";
        }
    });

    connect(startRegisterBtn,&QPushButton::clicked,this,[=](){
        QVariantMap infoMap;
        QString username = reusername->text();
        QString userpasswd= repasswd->text();
        if(!username.isEmpty()&&!userpasswd.isEmpty())
        {
            infoMap["userName"]=username;
            infoMap["passwd"]=userpasswd;
            infoMap["nickName"]="test";
            HttpHelper::instance()->slot_Register(infoMap);
        }
        else
        {
            qDebug()<<"注册的账号密码不能为空";
        }
    });


    connect(registerBtn,&QPushButton::clicked,this,[this](){
        stackedWgt->setCurrentWidget(registerWgt);
    });

    stackedWgt->setCurrentWidget(loginWgt);

    this->setLayout(mainlayout);

    StyleLoader::getInstance()->loadStyle(":/qss/main.css",this);

}
